package com.example.warehouse.sys.dao.persist.respository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.util.PageInfoToPageDataConverter;
import com.example.warehouse.sys.dao.persist.mapper.DeptMapper;
import com.example.warehouse.sys.dao.persist.respository.IDeptRepository;
import com.example.warehouse.sys.pojo.entity.SysDept;
import com.example.warehouse.sys.pojo.entity.SysUser;
import com.example.warehouse.sys.pojo.vo.DeptListItemVO;
import com.example.warehouse.sys.pojo.vo.DeptStandardVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Slf4j
@Repository
public class DeptRepositoryImpl implements IDeptRepository {

    @Autowired
    private DeptMapper mapper;

    @Override
    public int insert(SysDept dept) {

        return mapper.insert(dept);
    }

    @Override
    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public int updateById(SysDept dept) {
        return mapper.updateById(dept);
    }

    @Override
    public int countByName(String name) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        return mapper.selectCount(wrapper);
    }

    @Override
    public int countByNameAndNotId(Long id, String name) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name).ne("id", id);;
        return mapper.selectCount(wrapper);
    }

    @Override
    public DeptStandardVO getStandardById(Long id) {
        return mapper.getStandardById(id);
    }



    @Override
    public PageData<DeptListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询标签列表】，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DeptListItemVO> list = mapper.list();
        PageInfo<DeptListItemVO> pageInfo = new PageInfo<>(list);
        PageData<DeptListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<DeptListItemVO> listByParentId(Long parentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DeptListItemVO> list = mapper.listByParentId(parentId);
        PageInfo<DeptListItemVO> pageInfo = new PageInfo<>(list);
        PageData<DeptListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public List<SysDept> selectAll() {
        List<SysDept> list = mapper.selectList(null);
        return list;
    }
}
